package com.example.wudi.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.wudi.dao.RoleDao;
import com.example.wudi.dao.RouterDao;
import com.example.wudi.entity.Permissions;
import com.example.wudi.entity.Role;
import com.example.wudi.entity.Router;
import com.example.wudi.entity.User;
import com.example.wudi.mapper.PermissionsMapper;
import com.example.wudi.mapper.RoleMapper;
import com.example.wudi.mapper.RouterMapper;
import com.example.wudi.service.RoleService;
import com.example.wudi.service.RouterService;
import com.example.wudi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Stream;

@Service
public class RouterServiceImpl implements RouterService {

    @Autowired
    private RouterMapper routerMapper;
    @Autowired
    private PermissionsMapper permissionsMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    @Override
    public List<RouterDao> findAll(String userId) {
        final int id = Integer.parseInt(userId);
        final User userById = userService.findUserById(id);
        final String roleId = userById.getRoleId();
        final RoleDao roleByUserId = roleService.getRoleByUserId(roleId);
        final List<Permissions> permissionsList = roleByUserId.getPermissionsList();

        List<Long> filterRouter = new ArrayList<>();
        for (int t = 0; t<permissionsList.size();t++) {
            final Permissions permissions = permissionsList.get(t);
            final String[] splitArr = permissions.getPermissionsPath().split(":");
            if (splitArr.length > 1 &&splitArr[1].equals("select") && permissions.getRouterId() != null) {
                filterRouter.add(permissions.getRouterId());
            }
        }
        List<Router> has = new ArrayList<>();
        for (int n = 0;n<filterRouter.size();n++) {
            QueryWrapper<Router> hsaQuery = new QueryWrapper<>();
            hsaQuery.eq("id", filterRouter.get(n));
            has.add(routerMapper.selectOne(hsaQuery));
        }

        List<RouterDao> list = new ArrayList<>();
        final Stream<Router> sorted = has.stream().sorted(Comparator.comparing(Router::getRouterOrder));
        sorted.forEach(item -> {
            RouterDao routerDao = new RouterDao();
            if(item.getParentId().equals("-1")) {
                routerDao.setId(item.getId());
                routerDao.setIcon(item.getIcon());
                routerDao.setName(item.getName());
                routerDao.setComponent(item.getComponent());
                routerDao.setPath(item.getPath());
                routerDao.setRedirect(item.getRedirect());
                routerDao.setParentId(item.getParentId());
                routerDao.setLabel(item.getLabel());
                routerDao.setRouterOrder(item.getRouterOrder());
                QueryWrapper<Router> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("parent_id", item.getId());
                final List<Router> routers1 = routerMapper.selectList(queryWrapper1);
                final Stream<Router> sorted1 = routers1.stream().sorted(Comparator.comparing(Router::getRouterOrder));
                routerDao.setRouterChildList(routers1.size() > 0? sorted1 : null);
                list.add(routerDao);
                System.out.println("hahaha" + list);
            }
        });
        return list;
    }

    @Override
    public Router finnOneById(String id) {
        QueryWrapper<Router> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        final Router router = routerMapper.selectOne(queryWrapper);
        return router;
    }

    @Override
    public int addRouter(Router router) {
        List<String> l = new ArrayList<>();
        l.add("add");
        l.add("update");
        l.add("select");
        l.add("delete");

        List<String> l2 = new ArrayList<>();
        l2.add("增加权限");
        l2.add("修改权限");
        l2.add("查询权限");
        l2.add("删除权限");

        final int insert = routerMapper.insert(router);
        QueryWrapper<Router> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("path", router.getPath());
        final Router router1 = routerMapper.selectOne(queryWrapper);


        for (int i = 0; i< l.size();i++) {
            Permissions permissions = new Permissions();
            permissions.setRouterId(router1.getId());
            permissions.setPermissionsName(router1.getLabel() + l2.get(i));
            permissions.setPermissionsPath(router1.getName()+ ":" + l.get(i));
            permissionsMapper.insert(permissions);
        }

        QueryWrapper<Role> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("role_name", "超级管理员");
        final Role role = roleMapper.selectOne(queryWrapper3);

        String ios = "";
        final List<Permissions> list1 = permissionsMapper.selectList(null);
        for (int ia = 0;ia<list1.size();ia++) {
            ios += list1.get(ia).getId() + ",";
        }
        role.setPermissionsId(ios);
        final int update = roleMapper.update(role, queryWrapper3);

        return insert;
    }

    @Override
    public int update(Router router) {
        QueryWrapper<Router> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", router.getId());
        return routerMapper.update(router, queryWrapper);
    }

    @Override
    public int delete(Long id) {
        QueryWrapper<Router> queryrouter = new QueryWrapper<>();
        queryrouter.eq("parent_id", id);
        final List<Router> routers = routerMapper.selectList(queryrouter);
        if(routers.size() > 0) {
            return 899;
        }


        final int i = routerMapper.deleteById(id);

        QueryWrapper<Permissions> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("router_id", id);
        final int delete = permissionsMapper.delete(queryWrapper);

        QueryWrapper<Role> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("role_name", "超级管理员");
        final Role role = roleMapper.selectOne(queryWrapper1);

        String ios = "";
        final List<Permissions> list1 = permissionsMapper.selectList(null);
        for (int ia = 0;ia<list1.size();ia++) {
            ios += list1.get(ia).getId() + ",";
        }
        role.setPermissionsId(ios);
        final int update = roleMapper.update(role, queryWrapper1);

        return i;
    }

    @Override
    public List<Router> findNoAll() {
        final List<Router> routers = routerMapper.selectList(null);
        return routers;
    }
}
